import { dateFormat } from '../js/utils'
export default {
  data() {
    return {
      tableData: [],
      resData: {},
      listApi: '',
      whereData: {},
      sort: {},
      pageIndex: 1,
      pageSize: 15,
      totalCount: 100,
      isLoading: false
    }
  },
  methods: {
    sortList({ prop, order }) {
      let sort = {}
      sort[prop] = (order === 'descending') ? 'desc' : 'asc'
      this.whereData.sort = sort
      this.sort = {
        order_field: prop,
        order_type: (order === 'descending') ? 'desc' : 'asc'
      }
      this.fetchData()
    },
    fetchData() {
      if (this.listApi) {
        this.isLoading = true
        this.$http.get(this.listApi, {
          ...this.whereData,
          ...this.sort,
          page: this.pageIndex,
          limit: this.pageSize
        }).then(res => {
          this.isLoading = false
          if (res.status === 1) {
            this.resData = res.data
            this.tableData = res.data.list || res.data.data
            this.totalCount = res.data.countAll
          } else if (res.code === 0) {
            this.resData = res.data
            this.tableData = res.data.page.list
            this.totalCount = res.data.page.total
          }
        })
      }
    },
    exportData(name = 'data') {
      this.$http.get(this.listApi, {
        ...this.whereData,
        ...this.sort,
        page: this.pageIndex,
        page_size: this.pageSize,
        is_down: 1
      }, {
        responseType: 'blob'
      }).then(res => {
        let blob = new Blob([res])
        let reader = new FileReader()
        reader.readAsDataURL(blob)
        reader.onload = (e) => {
          let a = document.createElement('a')
          a.download = `${name}.xlsx`
          a.href = e.target.result
          document.body.appendChild(a)
          a.click()
          a.remove()
        }
      })
    },
    handleCurrentChange(val) {
      this.pageIndex = val
      this.fetchData()
    },
    handleDateChange(vals, field) {
      if (vals) {
        if (field) {
          if (typeof field === 'string') {
            this.whereData[field] = vals.join(' - ')
          } else {
            this.whereData[field[0]] = vals[0] / 1000
            this.whereData[field[1]] = vals[1] / 1000
          }
        } else {
          this.whereData.date_start = vals[0]
          this.whereData.date_end = vals[1]
          this.whereData.time_pay_range = vals.join(' - ')
        }
      } else {
        if (field) {
          if (typeof field === 'string') {
            this.whereData[field] = ''
          } else {
            this.whereData[field[0]] = ''
            this.whereData[field[1]] = ''
          }
        } else {
          this.whereData.date_start = ''
          this.whereData.date_end = ''
          this.whereData.time_pay_range = ''
        }
      }
      this.fetchData()
    },
    formatTime(row, column) {
      let value = row[column.property]
      if (typeof value === 'string' && value.indexOf('-') > -1) {
        return value
      }
      if (value) {
        return dateFormat('YYYY-mm-dd HH:MM:SS', value)
      }
      return '--'
    },
    navigateTo(url) {
      this.$router.push(url)
    }
  },
  mounted() {
    this.fetchData()
  }
}
